﻿using System;
using AspNetCore;

using AutoMapper;

using Logistics.Model;
using Logistics.PersonnelMatters;

using Microsoft.AspNetCore.Mvc;

using System.Collections.Generic;
using System.Drawing.Printing;
using System.Threading.Tasks;
using System.Xml.Linq;

using static IdentityServer4.Models.IdentityResources;

namespace Logistics.Web.Controllers
{
    public class PersonnelMattersController : Controller
    {
        private readonly IPersonnelMattersAppService _personnelMattersAppService;

        public PersonnelMattersController(IPersonnelMattersAppService personnelMattersAppService)
        {
            _personnelMattersAppService = personnelMattersAppService;
        }

        #region 人事面板
        /// <summary>
        /// 人事面板
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 离职人数
        /// </summary>
        /// <returns></returns>
        public int DimissionCount()
        {
            var count = _personnelMattersAppService.GetDimissionCount().Result;
            return count;
        }

        /// <summary>
        /// 总人数
        /// </summary>
        /// <returns></returns>
        public int Headcount()
        {
            var count = _personnelMattersAppService.GetHeadcount().Result;
            return count;
        }

        /// <summary>
        /// 入职人数
        /// </summary>
        /// <returns></returns>
        public int InductionCount()
        {
            var count = _personnelMattersAppService.GetInductionCount().Result;
            return count;
        }

        /// <summary>
        /// 获取员工年龄比
        /// </summary>
        /// <returns></returns>
        public List<AgeRatio> GeyAgeRatios()
        {
            var ageRatio = _personnelMattersAppService.GetEmployeeAgeRatio().Result;
            return ageRatio;
        }

        /// <summary>
        /// 获取员工年龄比
        /// </summary>
        /// <returns></returns>
        public List<AgeRatio> GeySeniorityRatio()
        {
            var ageRatio = _personnelMattersAppService.GetSeniorityRatio().Result;
            return ageRatio;
        }

        /// <summary>
        /// 获取员工年龄比
        /// </summary>
        /// <returns></returns>
        public List<NumberOfDepartments> GeyDepartment()
        {
            var dep = _personnelMattersAppService.GetDepartment().Result;
            return dep;
        }


        /// <summary>
        /// 获取离职人数
        /// </summary>
        /// <returns></returns>
        public List<AgeRatio> GetNumberOfSeparations()
        {
            var dep = _personnelMattersAppService.GetNumberOfSeparations().Result;
            return dep;
        }

        #endregion


        /// <summary>
        /// 获取部门集合
        /// </summary>
        /// <returns></returns>
        public IActionResult GetDeptinfoList()
        {
            var dep = _personnelMattersAppService.GetDeptinfoList().Result;
            return Json(dep);
        }

        /// <summary>
        /// 获取职业集合
        /// </summary>
        /// <returns></returns>
        public IActionResult GetPositionInfoList(int id)
        {
            var posi = _personnelMattersAppService.GetPositionInfoList(id).Result;
            return Json(posi);
        }


        #region 人事登记
        /// <summary>
        /// 人员登记视图
        /// </summary>
        /// <returns></returns>
        public IActionResult EmployeeRegistrationView()
        {
            return View();
        }

        /// <summary>
        /// 获取员工数据
        /// </summary>
        /// <param name="name">通过员工名称进行模糊查询</param>
        /// <param name="phone">通过员工手机号进行模糊查询</param>
        /// <param name="departmentId">通过部门进行查询</param>
        /// <param name="category">通过类别进行查询</param>
        /// <param name="positionId">通过职位进行查询</param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetBasicList(string name = "", string phone = "", int departmentId = 0, string category = "", int positionId = 0, int page = 1, int limit = 5)
        {
            var list = _personnelMattersAppService.GetBasicList(name, phone, departmentId, category, positionId,
                page, limit).Result;
            return Json(new { code = "0", msg = "", count = list.Toacl, data = list.Item });
        }

        /// <summary>
        /// 删除员工数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelBasicById(string id)
        {
            var i = _personnelMattersAppService.DelBasicById(id).Result;
            return i;
        }

        /// <summary>
        /// 添加人员视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddBasicView()
        {
            return View();
        }

        /// <summary>
        /// 新增员工
        /// </summary>
        /// <param name="basic"></param>
        /// <returns></returns>
        public int AddBasic(BasicDto basic)
        {
            int i = _personnelMattersAppService.AddBasic(basic).Result;
            return i;
        }

        /// <summary>
        /// 反填员工视图
        /// </summary>
        /// <returns></returns>
        public IActionResult BackFillingView(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 根据编号获取员工数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Basic GetBasicById(int id)
        {
            var bas = _personnelMattersAppService.GetBasicById(id).Result;
            return bas;
        }

        /// <summary>
        /// 修改员工视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ModifyBaView(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="basic"></param>
        /// <returns></returns>
        public int PostBasic(Basic basic)
        {
            int i = _personnelMattersAppService.PutBasic(basic).Result;
            return i;
        }
        #endregion

        #region 入职

        /// <summary>
        /// 入职视图
        /// </summary>
        /// <returns></returns>
        public IActionResult InductionView()
        {
            return View();
        }


        /// <summary>
        /// 添加入职视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddEntryView()
        {
            return View();
        }

        /// <summary>
        /// 通过部门编号获取该部门的员工数据
        /// </summary>
        /// <param name="depId"></param>
        /// <returns></returns>
        public IActionResult GetBasicByDepId(int depId)
        {
            var basic = _personnelMattersAppService.GetBasicByDepId(depId).Result;
            return Json(basic);
        }

        /// <summary>
        /// 新增入职数据
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public IActionResult AddEntryData(Entry entry)
        {
            var i = _personnelMattersAppService.AddEntryData(entry).Result;
            return Ok(i);
        }

        /// <summary>
        /// 员工入职
        /// </summary>
        /// <param name="name"></param>
        /// <param name="depId"></param>
        /// <param name="postId"></param>
        /// <param name="state"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetEntryList(string name=null, int depId=0, int postId=0, int state=-1, int page=1,
            int limit=5)
        {
            var list = _personnelMattersAppService.GetEntryList(name, depId, postId, state, page, limit).Result;
            return Json(new { code = "0", msg = "", count = list.Toacl, data = list.Item });
        }

        /// <summary>
        /// 通过编号删除入职数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult DelEntry(string id)
        {
            var i = _personnelMattersAppService.DelEntryById(id).Result;
            return Ok(i);
        }

        /// <summary>
        /// 查看入职数据视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult SeeEntryView(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 通过编号获取员工数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IActionResult GetEntryData(int id, int state)
        {
            var obj = _personnelMattersAppService.GetEntryData(id,state).Result;
            return Json(obj);
        }

        /// <summary>
        /// 反填入职数据视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult BackEntryView(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 修改入职数据
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public IActionResult PutEntry(Entry entry)
        {
            int i = _personnelMattersAppService.PutEntry(entry).Result;
            return Ok(i);
        }

        /// <summary>
        /// 提交入职申请
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult SubmitEntry(int id)
        {
            int i = _personnelMattersAppService.SubmitEntry(id).Result;
            return Ok(i);
        }

        #endregion

        #region 转正

        /// <summary>
        /// 转正视图
        /// </summary>
        /// <returns></returns>
        public IActionResult BecomeARegularWorkerView()
        {
            return View();
        }


        /// <summary>
        /// 添加转正视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddBecomeView()
        {
            return View();
        }


        /// <summary>
        /// 新增转正数据
        /// </summary>
        /// <param name="become"></param>
        /// <returns></returns>
        public IActionResult AddBecomeData(Become become)
        {
            var i = _personnelMattersAppService.AddBecomeData(become).Result;
            return Ok(i);
        }

        /// <summary>
        /// 员工转正
        /// </summary>
        /// <param name="name"></param>
        /// <param name="depId"></param>
        /// <param name="postId"></param>
        /// <param name="state"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetBecomeList(string name = null, int depId = 0, int postId = 0, int state = -1, int page = 1,
            int limit = 5)
        {
            var list = _personnelMattersAppService.GetBecomeList(name, depId, postId, state, page, limit).Result;
            return Json(new { code = "0", msg = "", count = list.Toacl, data = list.Item });
        }

        /// <summary>
        /// 通过编号删除转正数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult DelBecome(string id)
        {
            var i = _personnelMattersAppService.DelBecomeById(id).Result;
            return Ok(i);
        }

        /// <summary>
        /// 查看转正数据视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult SeeBecomeView(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 通过编号获取员工数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IActionResult GetBecomeData(int id, int state)
        {
            var obj = _personnelMattersAppService.GetBecomeData(id, state).Result;
            return Json(obj);
        }

        /// <summary>
        /// 反填转正数据视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult BackBecomeView(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 修改转正数据
        /// </summary>
        /// <param name="become"></param>
        /// <returns></returns>
        public IActionResult PutBecome(Become become)
        {
            int i = _personnelMattersAppService.PutBecome(become).Result;
            return Ok(i);
        }

        /// <summary>
        /// 提交转正申请
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult SubmitBecome(int id)
        {
            int i = _personnelMattersAppService.SubmitBecome(id).Result;
            return Ok(i);
        }

        #endregion

        #region 离职

        /// <summary>
        /// 离职视图
        /// </summary>
        /// <returns></returns>
        public IActionResult QuitView()
        {
            return View();
        }


        /// <summary>
        /// 添加离职视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddDimissionView()
        {
            return View();
        }


        /// <summary>
        /// 新增离职数据
        /// </summary>
        /// <param name="dimission"></param>
        /// <returns></returns>
        public IActionResult AddDimissionData(Dimission dimission)
        {
            var i = _personnelMattersAppService.AddDimissionData(dimission).Result;
            return Ok(i);
        }

        /// <summary>
        /// 员工离职
        /// </summary>
        /// <param name="name"></param>
        /// <param name="depId"></param>
        /// <param name="postId"></param>
        /// <param name="state"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetDimissionList(string name = null, int depId = 0, int postId = 0, int state = -1, int page = 1,
            int limit = 5)
        {
            var list = _personnelMattersAppService.GetDimissionList(name, depId, postId, state, page, limit).Result;
            return Json(new { code = "0", msg = "", count = list.Toacl, data = list.Item });
        }

        /// <summary>
        /// 通过编号删除离职数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult DelDimission(string id)
        {
            var i = _personnelMattersAppService.DelDimissionById(id).Result;
            return Ok(i);
        }

        /// <summary>
        /// 查看离职数据视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult SeeDimissionView(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 通过编号获取员工数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IActionResult GetDimissionData(int id, int state)
        {
            var obj = _personnelMattersAppService.GetDimissionData(id, state).Result;
            return Json(obj);
        }

        /// <summary>
        /// 反填离职数据视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult BackDimissionView(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 修改离职数据
        /// </summary>
        /// <param name="dimission"></param>
        /// <returns></returns>
        public IActionResult PutDimission(Dimission dimission)
        {
            int i = _personnelMattersAppService.PutDimission(dimission).Result;
            return Ok(i);
        }

        /// <summary>
        /// 提交离职申请
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult SubmitDimission(int id)
        {
            int i = _personnelMattersAppService.SubmitDimission(id).Result;
            return Ok(i);
        }

        #endregion
    }
}
